﻿using DocumentFormat.OpenXml.Lite.Core;
using DocumentFormat.OpenXml.Lite.Helper;
using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Types;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Style
{
    [XmlRoot("tblStylePr", Namespace = Constants.WordMain)]
    public class TableStyleProperties : XmlSerializable
    {
        [XmlAttribute("type", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public ST_TblStyleOverrideType Type { get; set; }

        [Optional]
        [XmlElement("rPr", Namespace = Constants.WordMain)]
        public XmlList<IRunPropElement> RunProperties { get; set; }

        [Optional]
        [XmlElement("pPr", Namespace = Constants.WordMain)]
        public XmlList<IParagraphPropElement> ParagraphProperties { get; set; }

        [Optional]
        [XmlElement("tblPr", Namespace = Constants.WordMain)]
        public XmlList<ITablePropElement> TableProperties { get; set; }

        [Optional]
        [XmlElement("trPr", Namespace = Constants.WordMain)]
        public XmlList<ITableRowPropElement> TableRowProperties { get; set; }

        [Optional]
        [XmlElement("tcPr", Namespace = Constants.WordMain)]
        public XmlList<ITableCellPropElement> TableCellProperties { get; set; }

        public TableStyleProperties() { }
        
        #region Methods

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddRunProperty(IRunPropElement element)
        {
            if (RunProperties == null)
            {
                RunProperties = new XmlList<IRunPropElement>();
                RunProperties.Changing += RunProperties_Changing;
            }

            var obj = RunProperties.FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as IRunPropElement).Copy(element);
            }
            else
            {
                RunProperties.Add(element);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddParagraphProperty(IParagraphPropElement element)
        {
            if (ParagraphProperties == null)
            {
                ParagraphProperties = new XmlList<IParagraphPropElement>();
                ParagraphProperties.Changing += ParagraphProperties_Changing;
            }

            var obj = ParagraphProperties.FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as IRunPropElement).Copy(element);
            }
            else
            {
                ParagraphProperties.Add(element);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddTableProperty(ITablePropElement element)
        {
            if (TableProperties == null)
            {
                TableProperties = new XmlList<ITablePropElement>();
                TableProperties.Changing += TableProperties_Changing;
            }

            var obj = TableProperties.FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as ITablePropElement).Copy(element);
            }
            else
            {
                TableProperties.Add(element);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddTableRowProperty(ITableRowPropElement element)
        {
            if (TableRowProperties == null)
            {

                TableRowProperties = new XmlList<ITableRowPropElement>();
                TableRowProperties.Changing += TableRowProperties_Changing;
            }

            var obj = TableRowProperties.FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as ITableRowPropElement).Copy(element);
            }
            else
            {
                TableRowProperties.Add(element);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddProperty(ITableCellPropElement element)
        {
            if (TableCellProperties == null)
            {
                TableCellProperties = new XmlList<ITableCellPropElement>();
                TableCellProperties.Changing += TableCellProperties_Changing;
            }

            var obj = TableCellProperties.FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as ITableCellPropElement).Copy(element);
            }
            else
            {
                TableCellProperties.Add(element);
                return true;
            }

            return false;
        }

        #endregion

        #region Events

        void RunProperties_Changing(object sender, ListEventArgs<IRunPropElement> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                if (obj.GetType().IsInheritedFrom(typeof(IRunPropElement)))
                {
                    var element = RunProperties.FirstOrDefault(e => e.GetType() == obj.GetType());
                    if (obj != null)
                    {
                        throw new OpenXmlException("Element with the same type (" +
                            element.GetType().ToString() + ") already added to list: " + RunProperties.ToString());
                    }
                }
            }
        }

        void ParagraphProperties_Changing(object sender, ListEventArgs<IParagraphPropElement> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                if (obj.GetType().IsInheritedFrom(typeof(IParagraphPropElement)))
                {
                    var element = ParagraphProperties.FirstOrDefault(e => e.GetType() == obj.GetType());
                    if (obj != null)
                    {
                        throw new OpenXmlException("Element with the same type (" +
                            element.GetType().ToString() + ") already added to list: " + ParagraphProperties.ToString());
                    }
                }
            }
        }

        void TableProperties_Changing(object sender, ListEventArgs<ITablePropElement> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                if (obj.GetType().IsInheritedFrom(typeof(ITablePropElement)))
                {
                    var element = TableProperties.FirstOrDefault(e => e.GetType() == obj.GetType());
                    if (obj != null)
                    {
                        throw new OpenXmlException("Element with the same type (" +
                            element.GetType().ToString() + ") already added to list: " + TableProperties.ToString());
                    }
                }
            }
        }

        void TableRowProperties_Changing(object sender, ListEventArgs<ITableRowPropElement> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                if (obj.GetType().IsInheritedFrom(typeof(ITableRowPropElement)))
                {
                    var element = TableRowProperties.FirstOrDefault(e => e.GetType() == obj.GetType());
                    if (obj != null)
                    {
                        throw new OpenXmlException("Element with the same type (" +
                            element.GetType().ToString() + ") already added to list: " + TableRowProperties.ToString());
                    }
                }
            }
        }

        void TableCellProperties_Changing(object sender, ListEventArgs<ITableCellPropElement> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                if (obj.GetType().IsInheritedFrom(typeof(ITableCellPropElement)))
                {
                    var element = TableCellProperties.FirstOrDefault(e => e.GetType() == obj.GetType());
                    if (obj != null)
                    {
                        throw new OpenXmlException("Element with the same type (" +
                            element.GetType().ToString() + ") already added to list: " + TableCellProperties.ToString());
                    }
                }
            }
        }

        #endregion
    }
}
